﻿#region Copyright (c) 2013-06, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

using System;
using System.Windows;


namespace Amarok.Presentation
{
	/// <summary>
	/// This type contains extension methods for Window.
	/// </summary>
	public static class WindowExtensions
	{
		#region WindowPosition

		/// <summary>
		/// Returns the position of the given window.
		/// </summary>
		/// 
		/// <param name="window">
		/// The window for which the position should be returned.</param>
		public static WindowPosition GetPosition(this Window window)
		{
			return new WindowPosition(
				window.Left,
				window.Top,
				window.Width,
				window.Height,
				window.WindowState == WindowState.Maximized);
		}


		/// <summary>
		/// Stores the position of the given window into the supplied WindowPosition object.
		/// </summary>
		/// 
		/// <param name="window">
		/// The window for which the position should be stored.</param>
		/// <param name="position">
		/// The WindowPosition object into which the current position should be stored.</param>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		public static void StorePosition(this Window window, WindowPosition position)
		{
			Verify.NotNull(position, "position");

			position.Left = window.Left;
			position.Top = window.Top;
			position.Width = window.Width;
			position.Height = window.Height;
			position.IsMaximized = window.WindowState == WindowState.Maximized;
		}


		/// <summary>
		/// Restores the position of the given window from the supplied WindowPosition object.
		/// </summary>
		/// 
		/// <param name="window">
		/// The window for which the position should be restored.</param>
		/// <param name="position">
		/// The WindowPosition object from which the current position should be restored.</param>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		public static void RestorePosition(this Window window, WindowPosition position)
		{
			Verify.NotNull(position, "position");

			position.SizeIntoFit();
			position.MoveIntoView();

			if (position.Width <= 0.0 || position.Height <= 0.0)
				return;

			window.Left = position.Left;
			window.Top = position.Top;
			window.Width = position.Width;
			window.Height = position.Height;
			window.WindowState = position.IsMaximized ? WindowState.Maximized : WindowState.Normal;
		}

		#endregion

	}
}
